<?php

/**
 * Created by PhpStorm.
 * User: liaoth3
 * Date: 2016/8/19
 * Time: 20:55
 */
namespace App\Http\Business;

use App\Exceptions\PriceException;
use App\Http\Service\CacheService;

class PriceBusiness
{
    public $cacheService;

    public function __construct($cacheService = null)
    {
        $this->cacheService = is_null($cacheService) ? new CacheService() : $cacheService;
    }

    /**
     * 签名校验
     * @return bool
     */
    public function checkSign()
    {
        return true;
    }

    /**
     * 自定义购买价格排序函数
     * @param $a array
     * @param $b array
     * @return bool
     */
    protected function _buyOrderCompare($a, $b)
    {
        $orderDiff = doubleval(config('app.price.sortDiff'));
        if (abs($a['unitAmount'] - $b['unitAmount']) > $orderDiff) {
            return $a['unitAmount'] < $b['unitAmount'];
        }

        return $a['credit'] < $b['credit'];
    }

    /**
     * 自定义出货价格排序函数
     * @param $a array
     * @param $b array
     * @return bool
     */
    protected function _sellOrderCompare($a, $b)
    {
        $orderDiff = doubleval(config('app.price.sortDiff'));
        if (abs($a['unitAmount'] - $b['unitAmount']) > $orderDiff) {
            return $a['unitAmount'] < $b['unitAmount'];
        }

        return $a['credit'] < $b['credit'];
    }

    /**
     * 从缓存中得到合并排序之后的可采购价格数据列表
     * @param $areaNumber int
     * @return array int
     * @throws PriceException
     */
    protected function _getBuyPriceList($areaNumber)
    {
        $priceList1771 = $this->cacheService->getBuyPriceList1771($areaNumber);
        $priceListUoo = $this->cacheService->getBuyPriceListUoo($areaNumber);

        $priceList = array();
        foreach ($priceList1771 as $v) {
            if (!empty($v)) {
                $priceList[] = $v;
            }
        }
        foreach ($priceListUoo as $v) {
            if (!empty($v)) {
                $priceList[] = $v;
            }
        }

        //如果没有可售金币，抛出异常
        if (empty($priceList)) {
            throw new PriceException(301);
        }

        //默认缓存中的所有的订单在自动域已经过过滤之,所以这边不再过滤处理,但是合并数据之后需要重新排序
        usort($priceList, array(__CLASS__, '_buyOrderCompare'));

        return $priceList;
    }

    /**
     * 从缓存中得到合并排序之后的可出货价格数据列表
     * @param $areaNumber int
     * @return array int
     * @throws PriceException
     */
    protected function _getSellPriceList($areaNumber)
    {
        $priceList1771 = $this->cacheService->getSellPriceList1771($areaNumber);
        $priceListUoo = $this->cacheService->getSellPriceListUoo($areaNumber);

        $priceList = array();
        foreach ($priceList1771 as $v) {
            if (!empty($v)) {
                $priceList[] = $v;
            }
        }
        foreach ($priceListUoo as $v) {
            if (!empty($v)) {
                $priceList[] = $v;
            }
        }

        //如果没有可售金币，抛出异常
        if (empty($priceList)) {
            throw new PriceException(302);
        }

        //默认缓存中的所有的订单在自动域已经过过滤之,所以这边不再过滤处理,但是合并数据之后需要重新排序
        usort($priceList, array(__CLASS__, '_sellOrderCompare'));

        return $priceList;
    }

    /**
     * 根据缓存数据得到采购价格
     * @param $areaNumber int
     * @param $goldAmount int
     * @return array
     * @throws PriceException
     */
    public function getBuyPrice($areaNumber, $goldAmount)
    {
        //得到缓存中对应大区的价格数据
        $priceList = $this->_getBuyPriceList($areaNumber);

        $sumGoldAmount = 0;
        $sumPrice = 0;
        //如果可发订单量大于订单量返回可发数量为订单量，如果小于订单量那么返回最大可发量
        for ($i = 0; $i < count($priceList); $i++) {
            if ($sumGoldAmount + $priceList[$i]['goldAmount'] >= $goldAmount) {
                $sumPrice = $sumPrice + (($goldAmount - $sumGoldAmount) * $priceList[$i]['price']);
                $sumGoldAmount = $goldAmount;
                break;
            }
            $sumGoldAmount += $priceList[$i]['goldAmount'];
            $sumPrice += ($priceList[$i]['goldAmount'] * $priceList[$i]['price']);
        }

        //设置的每笔订单的利润率
        $profit = config('app.price.profit');
        $unitPrice = round(($sumPrice / $sumGoldAmount) * (1 + $profit), 5);

        return array('areaNumber' => 0, 'goldAmount' => $sumGoldAmount, 'unitPrice' => $unitPrice);
    }

    /**
     * 根据缓存数据得到出货价格
     * @param $areaNumber int
     * @param $goldAmount int
     * @return array
     * @throws PriceException
     */
    public function getSellPrice($areaNumber, $goldAmount)
    {
        //得到缓存中对应大区的价格数据
        $priceList = $this->_getSellPriceList($areaNumber);

        $sumGoldAmount = 0;
        $sumPrice = 0;
        //如果可收货订单量大于订单量返回收货数量为订单量，如果小于订单量那么返回最大可收货量
        for ($i = 0; $i < count($priceList); $i++) {
            if ($sumGoldAmount + $priceList[$i]['goldAmount'] >= $goldAmount) {
                $sumPrice = $sumPrice + (($goldAmount - $sumGoldAmount) * $priceList[$i]['price']);
                $sumGoldAmount = $goldAmount;
                break;
            }
            $sumGoldAmount += $priceList[$i]['goldAmount'];
            $sumPrice += ($priceList[$i]['goldAmount'] * $priceList[$i]['price']);
        }

        //设置的每笔订单的利润率
        $profit = config('app.price.profit');
        $unitPrice = round(($sumPrice / $sumGoldAmount) * (1 + $profit), 5);

        return array('areaNumber' => 0, 'goldAmount' => $sumGoldAmount, 'unitPrice' => $unitPrice);
    }
}